package ants;

import java.io.IOException;
import java.io.InputStream;

/**
 * Handles system input stream reading.
 */
public abstract class AbstractSystemInputReader {

	/**
	 * Process a line read out by {@link #readSystemInput()} method in a way
	 * defined by subclass implementation.
	 * 
	 * @param line
	 *          single, trimmed line of system input
	 */
	public abstract void processLine(String line);

	/**
	 * Reads input stream line by line. All characters are converted to lower case
	 * and each line is passed for processing to {@link #processLine(String)}
	 * method.
	 */
	public void readSystemInput(InputStream in) throws IOException {
		StringBuilder line = new StringBuilder();
		int c;
		while ((c = in.read()) >= 0) {
			if (c == '\r' || c == '\n') {
				processLine(line.toString().toLowerCase().trim());
				line.setLength(0);
			} else {
				line = line.append((char) c);
			}
		}
	}
}
